//
//  ANNewsListViewController.m
//  AttitudeNews
//
//  Created by 刘晓萌 on 15/11/23.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "ANNewsListViewController.h"
#import "ANShowNewsViewController.h"
#import "ANPicsViewController.h"
#import "ANShowInfoTableView.h"
#import "ANNewsModel.h"
#import "Urls.h"
#import "MJRefresh.h"
#import "UIViewController+LLT_UIViewControllerTools.h"
#import "DirectionMenubarView.h"
@interface ANNewsListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *titleView;

@property (weak, nonatomic) IBOutlet UIView *ANShowView;
@property (weak, nonatomic) IBOutlet UILabel *headTitle;
@property (strong, nonatomic) NSMutableArray *titileArray;
@property (strong, nonatomic) UICollectionView *collection;
@property (strong, nonatomic) DirectionMenubarView *dicm;

@end

@implementation ANNewsListViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self creatCollectionView];
    
    [self.ANShowView addSubview:self.collection];
    
    NSArray *arr = @[@"科技",@"游戏",@"逸闻",@"历史",@"贵圈"];
    self.dicm = [DirectionMenubarView instntceWithFrame:CGRectMake(0, 0, WIDTH, 35) andTitleArr:arr.mutableCopy];
    [self.titleView addSubview:self.dicm];
    __weak typeof(self) __weakSelf = self;
    self.dicm.doChang = ^{
        __weakSelf.collection.contentOffset = CGPointMake(([__weakSelf.dicm getTapedIndex]*WIDTH),__weakSelf.collection.frame.origin.y);
    };
}


- (IBAction)didClickLeftButton:(UIButton *)sender {
    DissMissBack;
}


/**
 * 设置处理collectionView
 */
-(void)creatCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(-150, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    UICollectionView *collection = self.collection;
    collection.dataSource = self;
    collection.delegate = self;
    self.titileArray = [NSMutableArray array];
    self.titileArray = @[Urls_ANTech,Urls_ANGame,Urls_ANAnecdote,Urls_ANHistory,Urls_ANEntertainment].mutableCopy;
    for (NSString *identyfier in self.titileArray) {
        [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identyfier];
    }
    collection.pagingEnabled = YES;
    
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger index = self.collection.contentOffset.x/self.ANShowView.frame.size.width;
    [self.dicm changTitleWithIndex:index];
    
}
#pragma mark --- 实现collectionView的方法

/**
 * 设置元素的个数
 */
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 5;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;
}

/**
 * 初始化cell
 */
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *identfier = self.titileArray[indexPath.section];
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identfier forIndexPath:indexPath];
    CGFloat width = cell.contentView.frame.size.width;
    CGFloat height = cell.contentView.frame.size.height;
    CGRect tableFrame = CGRectMake(0, 20, width, height+20);
    ANShowInfoTableView *infoTable = [ANShowInfoTableView instanceWithFrame:tableFrame andTitleName:identfier];
    [infoTable setPushBlock:^(ANNewsModel *newsModel){
        if ([newsModel.skipID componentsSeparatedByString:@"|"].count == 2) {
            //NSLog(@"%@",newsModel.skipID);
            UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"ANMain" bundle:nil];
            
            ANPicsViewController *picsView = [mainStoryboard instantiateViewControllerWithIdentifier:@"picsVC"];
            picsView.newsModel = newsModel;
            [self presentViewController:picsView animated:YES completion:nil];
        }else{
            UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"ANMain" bundle:nil];
            ANShowNewsViewController *newsVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"newsVC"];
            NSArray *arr = @[@"科技",@"游戏",@"逸闻",@"历史",@"贵圈"];
            newsModel.headTitle = arr[[self.dicm getTapedIndex]];

            newsVC.newsModel = newsModel;
            [self presentViewController:newsVC animated:YES completion:nil];
        }
    }];
    [cell.contentView addSubview:infoTable];
    return cell;
}

/**
 * 设置每一个item的大小
 */
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(ANSHOWWidth,ANSHOWHeigt+55);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
